<?php

namespace app\common\model;

use think\Model;
use think\Db;
use app\common\lib\Redis;

class Rec extends Model
{

    protected $name = 'member_rec';

    public static function getCount($where)
    {
        try {
            return (new Rec)->where($where)->count();

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public static function get_my_team($uid, $page, $algebra)
    {
        try {

            return (new Rec)->where(['member_id' => $uid, 'algebra' => $algebra])
                ->alias('r')
                ->join('member m', 'r.new_member_id = m.id')
                ->field('r.*,m.nickname,m.mobile,m.photo,m.is_active')
                ->page($page, 20)
                ->order('create_time desc')
                ->select();

        } catch (\Exception $e) {
            return false;
        }
    }

    public static function getInfo($where, $field)
    {
        try {
            return (new Rec)->where($where)->field($field)->find();

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }

    public static function renew($uid, $amount, $field = 'team_achievement')
    {
        $all_id = parent::where('new_member_id', $uid)->column('member_id');
        Member::where('id', 'in', $all_id)->setInc($field, $amount);
    }
    //推荐奖
    public  function rec_award($uid, $order,$amount,$down,$up,$group)
    {
        try {
                $list = parent::alias('r')
                ->where(['r.new_member_id' => $uid])
                ->join('user m', 'r.member_id = m.id','left')
                ->field('r.member_id,r.algebra,m.user_type,m.order_num')->order('algebra', 'asc')->select();
                if($group){
                    $oid_arr=json_decode($group['oid'],true);
                }else{
                    $oid_arr=[];
                }
                $ing=array_merge($down,$up,$oid_arr);
                $ing_uid=Db::name('order_group')->where(['id'=>['in',$ing]])->column('uid');
                foreach ($list as $k => $v) {
                        //查询会员是否在排队中
                        // $group=Redis::Instance()->hGetAll('group');
                      
                        //推荐奖
                        if(in_array($v['member_id'],$ing_uid)){
                            $money=$amount*config('rec_rate');
                            Account::add($v['member_id'], 'balance', $money, '直推奖', '+', $uid, 1, $order);
                            break;
                        }
                    //团队奖
                    // if ($v['star_id'] && $p_query['team'.$v['star_id']] > $now_tp && $v['star_id']<4) {
                    //     $money = $p_query['team'.$v['star_id']] - $now_tp;
                    //     if ($money && $money > 0) {
                    //         // echo $money.'团队奖';
                    //         Account::add($v['member_id'], 'bonus', $money, '团队奖', '+', $uid, 2, 1,$order_id);
                    //         $now_tp =$p_query['team'.$v['star_id']];
                    //     }
                    //     //查询平级
                    //     $up_star=$list[$k+1]['star_id'];
                    //     if($p_query['team'.$up_star]==$now_tp){
                    //         $money=$p_query['level'];
                    //         // echo $money.'平级奖';
                    //         Account::add($list[$k+1]['member_id'], 'bonus', $money, '平级奖', '+', $uid, 3, 1,$order_id); 
                    //     }
                    // }
                    // //服务费
                    // if($v['is_server']==1 && $is_server==0){
                    //     $money=$p_query['server'];
                    //     // echo $money.'服务费';
                    //     Account::add($v['member_id'], 'bonus', $money, '服务费', '+', $uid, 4, 1,$order_id);
                    //     $is_server=1;
                    // }
                    // //升级
                    // if($v['star_id']<4){
                    //     $rec_vip=$v['rec'];
                    //     $rec_now=$v['star_id']==0?0:$v['rec'.$v['star_id']];
                    //     if($k==0){
                    //         $rec_vip+=1;
                    //     }
                       
                    //     if($rec_vip>=$level[$v['star_id']]['tp_1'] && $rec_now>=$level[$v['star_id']]['tp_next'] && $v['team_achievement']>=$level[$v['star_id']]['team_amount']){
                    //         Member::update(['star_id' => $v['star_id']+1], ['id' => $v['member_id']]);
                    //         Logs::adds('ID'.$v['member_id'] . '会员升级为:' . $level[$v['star_id']]['title']);
                    //         // echo 11;

                    //         $list[$k+1]['rec'.($v['star_id']+1)]+=1;//充值直推级别数量

                    //     }
                    // }
                }
            return ['code' => 1];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function adminBonus($uid, $amount)
    {
        try {

            $list = parent::with('user2')->where('new_member_id', $uid)
                ->where('algebra', '<', 3)
                ->field('member_id,algebra')->order('algebra', 'asc')->select();
            foreach ($list as $k => $v) {
                $money = round($amount * cache('db_config_data')['gl_tp_' . $v['algebra']], 2);
                if ($money && $money > 0) {
                    Account::add($v['member_id'], 'bonus', $money, '管理奖', '+', $uid, 3, 1);
                }
            }

            return true;

        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }


    public static function leaderBonus($uid, $amount)
    {
        try {

            $list = parent::with('user2')->where('new_member_id', $uid)
                ->where('algebra', '<', 3)
                ->field('member_id,algebra')->order('algebra', 'asc')->select();
            foreach ($list as $k => $v) {
                $money = round($amount * cache('db_config_data')['leader_tp_' . $v['algebra']], 2);
                if ($money && $money > 0) {
                    Account::add($v['member_id'], 'bonus', $money, '领导奖', '+', $uid, 8, 1);
                }
            }

            return true;

        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    public function user2()
    {
        return $this->hasOne('Member', 'id', 'member_id')->field('id,code,star_id,team_achievement');
    }


    /**
     * @param $member_id
     * @param $new_code
     * @param $status
     * @return int
     * 记录推荐关系
     */
    public static function recommend($uid, $new_code, $referee_code, $status = 1)
    {
        try {
            $algebra = 1;
            $time = time();
            if ($referee_code) {
                $p_code = $referee_code;
                $array = [];
                while ($p_member = User::get(['code' => $p_code])) {

                    if ($p_member) {
                        $data['member_id'] = $p_member['id'];
                        $data['code'] = $p_member['code'];
                        $data['create_time'] = time();
                        $data['new_member_id'] = $uid;
                        $data['new_code'] = $new_code;
                        $data['algebra'] = $algebra;
                        $data['create_time'] = $time;
                        $data['status'] = $status;
                        $array[] = $data;

                        $p_code = $p_member['referee_code'];
                    }
                    $algebra++;
                }

                (new Rec)->insertAll($array);
            }
            return true;
        } catch (\Exception $e) {

            return $e->getMessage();
        }
    }

}

